package com.shihang.crm.service;

import com.shihang.crm.bean.User;
import com.shihang.crm.dao.UserDao;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = true)
public class UserServiceImpl implements UserService{

    private UserDao userDao;
    @Override
    public User getUserByPassword(User u) {
        //1根据登录名称查询登录用户
        User existUser = userDao.getByUserCode(u.getUser_code());
        //User existUser = userDao.getById(u.getUser_code())
        //2.判断用户是否存在，不存在则抛出异常，提示用户名不存在
        if(null==existUser){
            throw new RuntimeException("用户名不存在！");
        }
        //3.判断密码是否正确，不正确则抛出异常，提示密码错误
        if(!u.getUser_password().equals(existUser.getUser_password())){
            throw new RuntimeException("密码错误！");
        }
        //4返回查询到的用户对象

        return existUser;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)
    public void saveUser(User u) {
        userDao.save(u);

    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
